package com.example.toutiao.view

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.example.toutiao.R
import kotlinx.android.synthetic.main.item_settings_view.view.*

class SettingItemView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    private var view:View?=null
    private var leftTitle:String?=""
    private var rightTitle:String?=""
    private var contentTitle:String?=""
    private var isShowButton:Boolean=false
    private var isShowImage:Boolean=false
    private val settingsArray:TypedArray=context.obtainStyledAttributes(attrs, R.styleable.SettingsItem)
    init {
        leftTitle=settingsArray.getString(R.styleable.SettingsItem_leftTitle)
        rightTitle=settingsArray.getString(R.styleable.SettingsItem_rightTitle)
        contentTitle=settingsArray.getString(R.styleable.SettingsItem_contentTitle)
        isShowButton=settingsArray.getBoolean(R.styleable.SettingsItem_isShowButton,false)
        isShowImage=settingsArray.getBoolean(R.styleable.SettingsItem_isShowImage,false)
        settingsArray.recycle()
        initView()
    }

    private fun initView() {
        view=LayoutInflater.from(context).inflate(R.layout.item_settings_view,this)
        view!!.left_title.text=leftTitle
        view!!.right_title.text=rightTitle
        view!!.content_title.text=contentTitle
        isShowBtn(view!!)
        isShowImage(view!!)

    }

    private fun isShowBtn(view: View) {
        if(isShowButton){
            view.right_title.visibility=View.GONE
            view.btn_off.visibility=View.VISIBLE
        }else{
            view.btn_off.visibility=View.GONE
        }

    }
    private fun isShowImage(view: View) {
        if(isShowImage){
            view.right_title.visibility=View.GONE
            view.iv_right_arrow.visibility=View.VISIBLE
        }else{
            view.iv_right_arrow.visibility=View.GONE
        }

    }

    fun setBgSetItem(colorIds:Int){
        view!!.rl_item.setBackgroundColor(ContextCompat.getColor(context,colorIds))
    }
}